package com.fanrui.code.chess.mahjong.round;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import com.fanrui.code.util.logger.Stdout;
import com.fanrui.proto.cmd.Operation;
import com.fanrui.proto.cmd.ResPlayerOperation.Result;
import com.google.gson.annotations.Expose;

/**  
* @ClassName: Round  
* @Description: 麻将中行牌产生的回合
* @author bear    
*/ 
public abstract class Round {
	
	/**
	 * 回合需要等待的时间
	 */
	protected Integer waitTime = null;
	
	/**
	 * 这个回合操作的玩家
	 */
	@Expose
	protected Long currentId = null;
	
	/**
	 *是否已经完成
	 */
	protected AtomicBoolean end = new AtomicBoolean(false);
	
	/**
	 * 能够响应的操作
	 * */
	protected List<Operation> canResponseType = null;
	
	protected boolean isDoResponse = false;
	
	public Round(int waitTime,Long currentId,List<Operation> canResponseType) {
		this.waitTime = waitTime;
		this.currentId = currentId;
		this.canResponseType = canResponseType;
	}
	
	/**
	 * 玩家对回合的响应
	 */
	@Expose
	protected List<MahjongOperationResponse> playerResponse = new ArrayList<MahjongOperationResponse>();
	
	protected List<MahjongOperationResponse> copyPlayerResponse = new ArrayList<MahjongOperationResponse>();
	 
	/**  
	*  添加一条玩家响应
	*  @param respnse    
	*  @return  Object   
	*/  
	public  abstract  Result addResponse(MahjongOperationResponse response);
	
	
	/**  
	*  执行操作
	*  @return    
	*  Object    
	*/  
	public abstract Object doResponse();
	/**  
	*  是否可以响应某个操作
	*  @param type
	*  @return    
	*  boolean    
	*/  
	public Result canOperationType(MahjongOperationResponse response){
		if(response == null){
			Stdout.GET().info("响应的操作为null");
			return Result.CantOperation;
		}
		if(canResponseType == null || canResponseType.isEmpty()){
			Stdout.GET().info(getInfo()+"不能响应操作"+response.toString());
			return Result.CantOperation;
		}
		if(!canResponseType.contains(response.getType())){
			Stdout.GET().info("["+getInfo()+"]不能响应操作"+response.toString());
			return  Result.CantOperation;
		}
		if(playerResponse != null && !playerResponse.isEmpty()){
			synchronized (playerResponse) {
				for(MahjongOperationResponse mor:playerResponse){
					if(mor != null && mor.getOperationId() == response.getOperationId()){
						Stdout.GET().info(getInfo()+"已经响应过操作了."+"成功响应的操作：["+mor+"]本次响应：["+response.toString()+"]");
						return  Result.Operationed;
					}
				}
			}
			
		}
		Collection<Long> canResponseList = getCanResponseList();
		if(canResponseList == null || canResponseList.isEmpty() || !canResponseList.contains(response.getOperationId())){
			Stdout.GET().info(getInfo()+"不能响应此次操作.可以响应操作的玩家ID:"+canResponseList+"----请求的响应："+response);
			return  Result.DoNotHavePermissionOperation;
		}
		
		return  Result.Success;
		
	}
	public void addResponseToList(MahjongOperationResponse response){
		synchronized (playerResponse) {
			playerResponse.add(response);
		}

	}
	

	public Long getCurrentId() {
		return currentId;
	}
	
	public abstract void before();
	
	/**  
	*  可以响应的玩家列表
	*  @return    
	*  Collection<Long>    
	*/  
	public abstract Collection<Long> getCanResponseList();
	
	public abstract String getInfo();
}
